package wowraidmanager.client.auction.admin.maintain;

import java.util.ArrayList;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.auction.RealmListService;
import wowraidmanager.client.service.auction.RealmListServiceAsync;
import wowraidmanager.shared.model.auction.RealmList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AdminRealmList extends BaseComposite{

    private static RealmListUiBinder uiBinder = GWT.create(RealmListUiBinder.class);

    protected RealmListServiceAsync realmListService = GWT.create(RealmListService.class);
    
    interface RealmListUiBinder extends UiBinder<Widget, AdminRealmList>{
    }

    static int COLUMN_REALMNAME = 0;
    static int COLUMN_LASTMODIFIED = 1;
    static int COLUMN_UPDATETIME = 2;
    static int COLUMN_KEYHIDDEN = 3;
    
    static DateTimeFormat DATETIMEFORMAT = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_TIME_FULL);
    
    @UiField
    Button addButton;
    
    @UiField
    Button saveButton;
    
    @UiField
    FlexTable realmListFlextable;
    
    ArrayList<RealmList> realmListList;
    
    ArrayList<RealmList> setRealmListList;
    
    public AdminRealmList(){
        initWidget(uiBinder.createAndBindUi(this));
        setButton();
        getRealmList();
    }

    private void getRealmList(){
        realmListService.getRealmList(new AsyncCallback<ArrayList<RealmList>>(){
            
            public void onSuccess(ArrayList<RealmList> result){
                realmListList = result;
                setFlexTable();
            }
            
            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("RealmListの取得に失敗しました。");
            }
        });
    }

    private void setFlexTable(){
        int row = 0;
        realmListFlextable.setText(row, COLUMN_REALMNAME, "RealmName");
        realmListFlextable.setText(row, COLUMN_LASTMODIFIED, "LastModified");
        realmListFlextable.setText(row, COLUMN_UPDATETIME, "UpdateTime");
        
        row++;
        if(realmListList != null){
            for(RealmList realmList:realmListList){
                TextBox realmName = new TextBox();
                realmName.setText(realmList.getRealm());
                realmListFlextable.setWidget(row, COLUMN_REALMNAME, realmName);
                
                TextBox lastModified = new TextBox();
                lastModified.setText(realmList.getLastmodified());
                realmListFlextable.setWidget(row, COLUMN_LASTMODIFIED, lastModified);
                
                TextBox updateTime = new TextBox();
                if(realmList.getUpdateTime() != null){
                    updateTime.setText(DATETIMEFORMAT.format(realmList.getUpdateTime()));
                }else{
                    updateTime.setText("");
                }
                realmListFlextable.setWidget(row, COLUMN_UPDATETIME, updateTime);
                
                Hidden keyHidden = new Hidden();
                keyHidden.setValue(realmList.getKey().toString());
                realmListFlextable.setWidget(row, COLUMN_KEYHIDDEN, keyHidden);
                
                row++;
            }
        }
    }

    private void setButton(){
        addButton.addClickHandler(new ClickHandler(){
            
            public void onClick(ClickEvent event){
                addRealmList();
            }
        });
        
        saveButton.addClickHandler(new ClickHandler(){
            
            public void onClick(ClickEvent event){
                saveRealmList();
            }
        });
    }

    protected void saveRealmList(){
        setRealmListList = new ArrayList<RealmList>();
        for(int row = 1;row < realmListFlextable.getRowCount();row++){
            Hidden keyHidden = (Hidden)realmListFlextable.getWidget(row, COLUMN_KEYHIDDEN);

            RealmList realmList = null;
            if(!keyHidden.getValue().isEmpty()){
                for(RealmList oldRealmList:realmListList){
                    if(oldRealmList.getKey().toString().equals(keyHidden.getValue())){
                        realmList = oldRealmList;
                        break;
                    }
                }
            }else{
                realmList = new RealmList();
            }
            
            TextBox realmListTextBox = (TextBox)realmListFlextable.getWidget(row, COLUMN_REALMNAME);
            if(realmListTextBox.getText().isEmpty()){
                continue;
            }
            
            realmList.setRealm(realmListTextBox.getText());
            
            TextBox lastModifiedTextBox = (TextBox)realmListFlextable.getWidget(row, COLUMN_LASTMODIFIED);
            realmList.setLastmodified(lastModifiedTextBox.getText());
            
            TextBox updateTimeTextBox = (TextBox)realmListFlextable.getWidget(row, COLUMN_UPDATETIME);
            if(!updateTimeTextBox.getText().isEmpty()){
                realmList.setUpdateTime(DATETIMEFORMAT.parse(updateTimeTextBox.getText()));
            }
            
            setRealmListList.add(realmList);
        }
        
        realmListService.setRealmList(setRealmListList, new AsyncCallback<Void>(){
            
            public void onSuccess(Void result){
                setInfoMessage("RealmListの更新に成功しました。");
                clearErrorMessage();
            }
            
            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("RealmListの更新に失敗しました。");
            }
        });
    }

    protected void addRealmList(){
        int row = realmListFlextable.getRowCount();
        
        realmListFlextable.setWidget(row, COLUMN_REALMNAME, new TextBox());
        realmListFlextable.setWidget(row, COLUMN_LASTMODIFIED, new TextBox());
        realmListFlextable.setWidget(row, COLUMN_UPDATETIME, new TextBox());
        realmListFlextable.setWidget(row, COLUMN_KEYHIDDEN, new Hidden());
    }

}
